package com.alibaba.csp.sentinel.dashboard.rule.nacos.system;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.AbstractRuleNacosProvider;
import com.alibaba.csp.sentinel.dashboard.rule.nacos.SentinelNacosConstants;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author marcher
 */
@Component("systemRuleNacosProvider")
public class SystemRuleNacosProvider extends AbstractRuleNacosProvider<List<SystemRuleEntity>> {

    @Override
    public List<SystemRuleEntity> getRules(String appName,String ip,Integer port) throws Exception {
        List<SystemRule> rules = getOriginRules(appName, SystemRule.class);
        if (CollectionUtils.isEmpty(rules)) {
            return new ArrayList<>();
        }

        return rules.stream().map(rule -> SystemRuleEntity.fromSystemRule(appName, ip, port, rule)).collect(Collectors.toList());
    }

    @Override
    protected String postfix() {
        return SentinelNacosConstants.SYSTEM_DATA_ID_POSTFIX;
    }
}